<?php
/**
 * MatchDart! is a darts event ranking table component for joomla
 *
 * @version		2.0.1 | $Id: event.php 210 2010-06-14 21:07:35Z mwi.nor $
 * @package		MatchDart!
 * @subpackage	Site
 * @copyright	Copyright (C) 2005 - 2009 Marco Wilde. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * @author		Marco Wilde, <mailto://info@matchdart.de>
 * @link		http://www.matchdart.de
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class MatchdartModelEvent extends JModel
{
	protected $_event;
	protected $_eventID;
	protected $_lastUpdate;
	protected $_players;
	protected $_playoffMatches;
	protected $_season;
	protected $_seasonID;
	protected $_tableMatches;
	protected $_tablePlayerData;


	public function __construct()
	{
		parent::__construct();

		$app = &JFactory::getApplication();

		$config = JFactory::getConfig();

		// get params
		//		$params = &JComponentHelper::getParams( 'com_matchdart' );
		//		$this->_formatdate = $params->get('md_formatdate');

	}

	public function getSeasonID()
	{
		if (empty( $this->_seasonID))
		{
			$params = JComponentHelper::getParams('com_matchdart');
			$seasonid = $params->get('md_table_seasonid');

			if ( $seasonid != 0 ) {
				$this->_seasonID = $seasonid;
			} else {
				$query = 'SELECT SeasonID FROM #__md_seasons WHERE SeasonDefault = 1';
				$this->_db->setQuery( $query );
				$row = $this->_db->loadObject();

				$this->_seasonID = $row->SeasonID;
			}

		}

		return $this->_seasonID;

	}

	public function getEventID()
	{
		if (empty( $this->_eventID))
		{
			$params = JComponentHelper::getParams('com_matchdart');
			$eventid = $params->get('md_table_eventid');

			if ( $eventid != 0) {
				$this->_eventID = $eventid;
			} else {
				$query = 'SELECT EventID FROM #__md_events WHERE EventDefault = 1';
				$this->_db->setQuery( $query );
				$row = $this->_db->loadObject();
					
				$this->_eventID = $row->EventID;
			}

		}

		return $this->_eventID;

	}

	public function getSeason()
	{
		if (empty( $this->_season))
		{
			$id = $this->getSeasonID();

			$query = 'SELECT * FROM #__md_seasons WHERE SeasonID = '.(int) $id;
			$this->_db->setQuery( $query );
			$this->_season = $this->_db->loadObject();

		}

		return $this->_season;

	}

	public function getEvent()
	{
		if (empty( $this->_event))
		{
			$id = $this->getEventID();

			$query = 'SELECT * FROM #__md_events WHERE EventID = '.(int) $id;
			$this->_db->setQuery( $query );
			$this->_event = $this->_db->loadObject();

		}

		return $this->_event;

	}

	public function getPlayers()
	{
		// load the data if it doesn't already exist
		if (empty( $this->_players ))
		{

			$season = $this->getSeasonID();
			$event = $this->getEventID();

			$players = array();

			$query = 'SELECT DISTINCT'.
						' P.*'.
					' FROM'.
						' #__md_matches M,'.
						' #__md_players P'.
					' WHERE'.
						' M.MatchSeasonID LIKE '.$this->_db->Quote( $season ).' AND'.
						' M.MatchEventID LIKE '.$this->_db->Quote( $event ).' AND'.
						' (P.PlayerID = M.MatchHomePlayerID OR'.
						' P.PlayerID = M.MatchAwayPlayerID)'.
					' ORDER BY'.
						' P.PlayerID';

			$this->_players = $this->_getList($query);

		}
		return $this->_players;
	}

	public function getTeams()
	{
		// load the data if it doesn't already exist
		if (empty( $this->_teams ))
		{

			$season = $this->getSeasonID();
			$event = $this->getEventID();

			$teams = array();

			$query = 'SELECT DISTINCT'.
						' T.*'.
					' FROM'.
						' #__md_matches M,'.
						' #__md_teams T'.
					' WHERE'.
						' M.MatchSeasonID LIKE '.$this->_db->Quote( $season ).' AND'.
						' M.MatchEventID LIKE '.$this->_db->Quote( $event ).' AND'.
						' (T.TeamID = M.MatchHomeTeamID OR'.
						' T.TeamID = M.MatchAwayTeamID)'.
					' ORDER BY'.
						' T.TeamID';

			$this->_teams = $this->_getList($query);

		}
		return $this->_teams;
	}

	public function getTeamMatchDays()
	{
		// load the data if it doesn't already exist
		if (empty( $this->_teamMatchDays ))
		{

			$season = $this->getSeasonID();
			$event = $this->getEventID();

			$teams = array();

			$query = 'SELECT '.
						' MatchDate,'.
						' MatchHomeTeamID,'.
						' MatchAwayTeamID'.
					' FROM'.
						' #__md_matches'.
					' WHERE'.
						' MatchSeasonID LIKE '.$this->_db->Quote( $season ).' AND'.
						' MatchEventID LIKE '.$this->_db->Quote( $event ).
					' GROUP BY'.
						' CONCAT(MatchDate," ",MatchHomeTeamID," - ", MatchAwayTeamID)';

			$this->_teamMatchDays = $this->_getList($query);

		}
		return $this->_teamMatchDays;
	}

	public function getTableMatches()
	{
		if (empty( $this->_tableMatches ))
		{
			$season = $this->getSeasonID();
			$event = $this->getEventID();

			$query = 'SELECT'.
						' M.*,'.
						' E.*,'.
						' S.*'.
					' FROM'.
						' #__md_matches M'.
						' LEFT JOIN #__md_events E on (E.EventID = M.MatchEventID)'.
						' LEFT JOIN #__md_seasons S on (S.SeasonID = M.MatchSeasonID)'.
					' WHERE'.
						' E.EventPublish = 1 AND'.
						' S.SeasonPublish = 1 AND'.
        				' M.MatchType = 0 AND'.
						' M.MatchSeasonID LIKE '.$this->_db->Quote( $season ).' AND'.
						' M.MatchEventID LIKE '.$this->_db->Quote( $event );

			$this->_tableMatches = $this->_getList($query);

		}
		return $this->_tableMatches;
	}

	public function getPlayoffMatches()
	{
		if (empty( $this->_playoffMatches ))
		{
			$season = $this->getSeasonID();
			$event = $this->getEventID();

			$query = 'SELECT'.
						' M.*,'.
						' E.*,'.
						' S.*,'.
						' PH.PlayerName as HomePlayerName,'.
						' PA.PlayerName as AwayPlayerName'.
					' FROM'.
						' #__md_matches M'.
						' LEFT JOIN #__md_events E on (E.EventID = M.MatchEventID)'.
						' LEFT JOIN #__md_seasons S on (S.SeasonID = M.MatchSeasonID)'.
						' LEFT JOIN #__md_players PH on (PH.PlayerID = M.MatchHomePlayerID)'.
						' LEFT JOIN #__md_players PA on (PA.PlayerID = M.MatchAwayPlayerID)'.
					' WHERE'.
						' E.EventPublish = 1 AND'.
						' S.SeasonPublish = 1 AND'.
        				' M.MatchType <> 0 AND'.
						' M.MatchSeasonID LIKE '.$this->_db->Quote( $season ).' AND'.
						' M.MatchEventID LIKE '.$this->_db->Quote( $event ).
					' ORDER BY'.
						' M.MatchType';

			$this->_playoffMatches = $this->_getList($query);

		}
		return $this->_playoffMatches;
	}

	public function getLastUpdate()
	{
		if (empty( $this->_lastUpdate ))
		{
			$params = JComponentHelper::getParams('com_matchdart');
			$formatlastupdate = $params->get('md_formatlastupdate');

			$season = $this->getSeasonID();
			$event = $this->getEventID();

			$query ='SELECT'.
        			' MAX(MatchCreated) as LastUpdate'. 
        			' FROM'. 
        			' #__md_matches'. 
        			' WHERE'.
        				' MatchSeasonID LIKE '.$this->_db->Quote( $season ).' AND'.
						' MatchEventID LIKE '.$this->_db->Quote( $event );
			
			$this->_db->setQuery( $query );
			$timestamp = date( $formatlastupdate, $this->_db->loadResult() );
			$this->_lastUpdate = $timestamp;
		}
		return $this->_lastUpdate;
	}

	public function getTablePlayerData()
	{
		if (empty( $this->_tablePlayerData ))
		{
			$players = $this->getPlayers();
			$matches = $this->getTableMatches();

			$table = array();

			for($i = 0; $i < count($players); $i++)
			{

				$player =& $players[$i];

				$homewins = 0;
				$homewinpoints = 0;
				$homeloses = 0;
				$homelosepoints = 0;
				$homedraws = 0;
				$homedrawpoints = 0;
				$homesetswin = 0;
				$homesetslose = 0;
				$homelegswin = 0;
				$homelegslose = 0;
				$homelwatwin = 0;
				$homelwatlose = 0;
				$home100 = 0;
				$home140 = 0;
				$home180 = 0;
				$homehc = 0;

				$awaywins = 0;
				$awaywinpoints = 0;
				$awayloses = 0;
				$awaylosepoints = 0;
				$awaydraws = 0;
				$awaydrawpoints = 0;
				$awaysetswin = 0;
				$awaysetslose = 0;
				$awaylegswin = 0;
				$awaylegslose = 0;
				$awaylwatwin = 0;
				$awaylwatlose = 0;
				$away100 = 0;
				$away140 = 0;
				$away180 = 0;
				$awayhc = 0;

					
				//$table[$k]['PlayerName'] = $player->PlayerName;
				//$table[$k]['PlayerUID'] = $player->PlayerUID;

				foreach ($matches as $match)
				{
					if ($player->PlayerID == $match->MatchHomePlayerID)
					{

						switch ( $match->MatchResult )
						{
							case '0'  : // draw
								$homedraws++;
								$homedrawpoints = $homedrawpoints + $match->EventPointsDraw;
								break;
									
							case '1'  : // home win
								$homewins++;
								$homewinpoints = $homewinpoints + $match->EventPointsWin;
								break;

							case '2'  : // away win
								$homeloses++;
								$homelosepoints = $homelosepoints + $match->EventPointsLose;
								break;
						}

						$homesetswin = $homesetswin + $match->MatchHomeSets;
						$homesetslose = $homesetslose + $match->MatchAwaySets;
						$homelegswin = $homelegswin + $match->MatchHomeLegs;
						$homelegslose = $homelegslose + $match->MatchAwayLegs;
						$homelwatwin = $homelwatwin + $match->MatchHomeLWAT;
						$homelwatlose = $homelwatlose + $match->MatchAwayLWAT;
						$home100 = $home100 + $match->MatchHome100;
						$home140 = $home140 + $match->MatchHome140;
						$home180 = $home180 + $match->MatchHome180;
						$homehc = ($homehc <= $match->MatchHomeHC) ? $match->MatchHomeHC : $homehc;
							
					} elseif ($player->PlayerID == $match->MatchAwayPlayerID)
					{

						switch ( $match->MatchResult )
						{
							case '0'  : // draw
								$awaydraws++;
								$awaydrawpoints = $homedrawpoints + $match->EventPointsDraw;
								break;
									
							case '1'  : // home win
								$awayloses++;
								$awaylosepoints = $awaylosepoints + $match->EventPointsLose;
								break;

							case '2'  : // away win
								$awaywins++;
								$awaywinpoints = $awaywinpoints + $match->EventPointsWin;
								break;
						}

						$awaysetswin = $awaysetswin + $match->MatchAwaySets;
						$awaysetslose = $awaysetslose + $match->MatchHomeSets;
						$awaylegswin = $awaylegswin + $match->MatchAwayLegs;
						$awaylegslose = $awaylegslose + $match->MatchHomeLegs;
						$awaylwatwin = $awaylwatwin + $match->MatchAwayLWAT;
						$awaylwatlose = $awaylwatlose + $match->MatchHomeLWAT;
						$away100 = $away100 + $match->MatchAway100;
						$away140 = $away140 + $match->MatchAway140;
						$away180 = $away180 + $match->MatchAway180;
						$awayhc = ($awayhc <= $match->MatchAwayHC) ? $match->MatchAwayHC : $awayhc;

					}
				}

				$homematches = $homewins + $homeloses + $homedraws;
				$homepoints = $homewinpoints + $homedrawpoints + $homelosepoints;
				$awaymatches = $awaywins + $awayloses + $awaydraws;
				$awaypoints = $awaywinpoints + $awaydrawpoints + $awaylosepoints;

				$player->PlayerHomeMatches = $homematches;
				$player->PlayerHomePoints = $homepoints;
				$player->PlayerHomeWins = $homewins;
				$player->PlayerHomeDraws = $homedraws;
				$player->PlayerHomeLoses = $homeloses;
				$player->PlayerHomeSetsWin = $homesetswin;
				$player->PlayerHomeSetsLose = $homesetslose;
				$player->PlayerHomeSetsDiff = $homesetswin - $homesetslose;
				$player->PlayerHomeLegsWin = $homelegswin;
				$player->PlayerHomeLegsLose = $homelegslose;
				$player->PlayerHomeLegsDiff = $homelegswin - $homelegslose;
				$player->PlayerHomeLWATWin = $homelwatwin;
				$player->PlayerHomeLWATLose = $homelwatlose;
				$player->PlayerHome100 = $home100;
				$player->PlayerHome140 = $home140;
				$player->PlayerHome180 = $home180;
				$player->PlayerHomeHighfinish = $homehc;

				$player->PlayerAwayMatches = $awaymatches;
				$player->PlayerAwayPoints = $awaypoints;
				$player->PlayerAwayWins = $awaywins;
				$player->PlayerAwayDraws = $awaydraws;
				$player->PlayerAwayLoses = $awayloses;
				$player->PlayerAwaySetsWin = $awaysetswin;
				$player->PlayerAwaySetsLose = $awaysetslose;
				$player->PlayerAwaySetsDiff = $awaysetswin - $awaysetslose;
				$player->PlayerAwayLegsWin = $awaylegswin;
				$player->PlayerAwayLegsLose = $awaylegslose;
				$player->PlayerAwayLegsDiff = $awaylegswin - $awaylegslose;
				$player->PlayerAwayLWATWin = $awaylwatwin;
				$player->PlayerAwayLWATLose = $awaylwatlose;
				$player->PlayerAway100 = $away100;
				$player->PlayerAway140 = $away140;
				$player->PlayerAway180 = $away180;
				$player->PlayerAwayHighfinish = $awayhc;

				$player->PlayerTotalMatches = $homematches + $awaymatches;
				$player->PlayerTotalPoints = $homepoints + $awaypoints;
				$player->PlayerTotalWins = $homewins + $awaywins;
				$player->PlayerTotalDraws = $homedraws + $awaydraws;
				$player->PlayerTotalLoses = $homeloses + $awayloses;
				$player->PlayerTotalSetsWin = $homesetswin + $awaysetswin;
				$player->PlayerTotalSetsLose = $homesetslose + $awaysetslose;
				$player->PlayerTotalSetsDiff = $player->PlayerHomeSetsDiff + $player->PlayerAwaySetsDiff;
				$player->PlayerTotalLegsWin = $homelegswin + $awaylegswin;
				$player->PlayerTotalLegsLose = $homelegslose + $awaylegslose;
				$player->PlayerTotalLegsDiff = $player->PlayerHomeLegsDiff + $player->PlayerAwayLegsDiff;
				$player->PlayerTotalLWATWin = $homelwatwin + $awaylwatwin;
				$player->PlayerTotalLWATLose = $homelwatlose + $awaylwatlose;
				$player->PlayerTotal100 = $player->PlayerHome100 + $player->PlayerAway100;
				$player->PlayerTotal140 = $player->PlayerHome140 + $player->PlayerAway140;
				$player->PlayerTotal180 = $player->PlayerHome180 + $player->PlayerAway180;
				$player->PlayerTotalHighfinish = ($player->PlayerHomeHighfinish <= $player->PlayerAwayHighfinish ? $player->PlayerAwayHighfinish : $player->PlayerHomeHighfinish);

				$k++;

			}

			$players = JArrayHelper::sortObjects($players, 'PlayerTotalLegsDiff', 1);
			$players = JArrayHelper::sortObjects($players, 'PlayerTotalPoints', -1);

			$this->_tablePlayerData = $players;

		}
		return $this->_tablePlayerData;
	}

	public function getTableTeamData()
	{
		if (empty( $this->_tableTeamData ))
		{
			//$players = $this->getPlayers();
			$teams			= $this->getTeams();
			$teamMatchDays	= $this->getTeamMatchDays();
			$event			= $this->getEvent();
			
			foreach ($teams as $team)
			{
				foreach ($teamMatchDays as $teamMatchDay)
				{
						$tmdHomeWins = 0;
						$tmdHomeLoses = 0;
						$tmdHomeDraws = 0;
						
						$tmdAwayWins = 0;
						$tmdAwayLoses = 0;
						$tmdAwayDraws = 0;
						
					// home matches
					if ($team->TeamID == $teamMatchDay->MatchHomeTeamID)
					{
						// home match wins
						$query ='SELECT'.
        						' COUNT(*)'.
        					' FROM'. 
        						' #__md_matches'. 
        					' WHERE'.
								' MatchHomeTeamID = '.$teamMatchDay->MatchHomeTeamID.' AND'.
								' MatchAwayTeamID = '.$teamMatchDay->MatchAwayTeamID.' AND'.
        						' MatchDate = '.$this->_db->Quote($teamMatchDay->MatchDate).' AND'.
						' MatchResult = 1';
						$this->_db->setQuery( $query );
						$tmdHomeWins = $this->_db->loadResult();
						
						//home loses
						$query ='SELECT'.
        						' COUNT(*)'.
        					' FROM'. 
        						' #__md_matches'. 
        					' WHERE'.
								' MatchHomeTeamID = '.$teamMatchDay->MatchHomeTeamID.' AND'.
								' MatchAwayTeamID = '.$teamMatchDay->MatchAwayTeamID.' AND'.
        						' MatchDate = '.$this->_db->Quote($teamMatchDay->MatchDate).' AND'.
						' MatchResult = 2';
						$this->_db->setQuery( $query );
						$tmdHomeLoses = $this->_db->loadResult();

						//home draws
						$query ='SELECT'.
        						' COUNT(*)'.
        					' FROM'. 
        						' #__md_matches'. 
        					' WHERE'.
								' MatchHomeTeamID = '.$teamMatchDay->MatchHomeTeamID.' AND'.
								' MatchAwayTeamID = '.$teamMatchDay->MatchAwayTeamID.' AND'.
        						' MatchDate = '.$this->_db->Quote($teamMatchDay->MatchDate).' AND'.
						' MatchResult = 0';
						$this->_db->setQuery( $query );
						$tmdHomeDraws = $this->_db->loadResult();
						
						
						if (($tmdHomeWins - $tmdHomeLoses) > 0)
						{
							$team->HomeMatchWins ++;
						}
						else if (($tmdHomeWins - $tmdHomeLoses) < 0)
						{
							$team->HomeMatchLoses ++;
						}
						else
						{
							$team->HomeMatchDraws ++;
						}

						// home stats
						$query ='SELECT'.
        						' SUM(MatchHomeLWAT) as HomeLWAT,'.
        						' SUM(MatchHome100) as Home100,'.
        						' SUM(MatchHome140) as Home140,'.
        						' SUM(MatchHome180) as Home180,'.
        						' MAX(MatchHomeHC) as HomeHC'.
						' FROM'. 
        						' #__md_matches'. 
        					' WHERE'.
								' MatchHomeTeamID = '.$teamMatchDay->MatchHomeTeamID.' AND'.
								' MatchAwayTeamID = '.$teamMatchDay->MatchAwayTeamID.' AND'.
        						' MatchDate = '.$this->_db->Quote($teamMatchDay->MatchDate).' AND'.
						' MatchResult = 1';
						$tmdHomeStats = $this->_getList($query);
						
						
					} 
					// away matches
					if ($team->TeamID == $teamMatchDay->MatchAwayTeamID)
					{
						$teamid = $team->TeamID;
						$mtid	= $teamMatchDay->MatchAwayTeamID;
						
						$query ='SELECT'.
        						' COUNT(*)'.
        					' FROM'. 
        						' #__md_matches'. 
        					' WHERE'.
								' MatchHomeTeamID = '.$teamMatchDay->MatchHomeTeamID.' AND'.
								' MatchAwayTeamID = '.$teamMatchDay->MatchAwayTeamID.' AND'.
        						' MatchDate = '.$this->_db->Quote($teamMatchDay->MatchDate).' AND'.
						' MatchResult = 2';
						$this->_db->setQuery( $query );
						$tmdAwayWins = $this->_db->loadResult();
						
						$query ='SELECT'.
        						' COUNT(*)'.
        					' FROM'. 
        						' #__md_matches'. 
        					' WHERE'.
								' MatchHomeTeamID = '.$teamMatchDay->MatchHomeTeamID.' AND'.
								' MatchAwayTeamID = '.$teamMatchDay->MatchAwayTeamID.' AND'.
        						' MatchDate = '.$this->_db->Quote($teamMatchDay->MatchDate).' AND'.
						' MatchResult = 1';
						$this->_db->setQuery( $query );
						$tmdAwayLoses = $this->_db->loadResult();
												
						$query ='SELECT'.
        						' COUNT(*)'.
        					' FROM'. 
        						' #__md_matches'. 
        					' WHERE'.
								' MatchHomeTeamID = '.$teamMatchDay->MatchHomeTeamID.' AND'.
								' MatchAwayTeamID = '.$teamMatchDay->MatchAwayTeamID.' AND'.
        						' MatchDate = '.$this->_db->Quote($teamMatchDay->MatchDate).' AND'.
						' MatchResult = 0';
						$this->_db->setQuery( $query );
						$tmdAwayDraws = $this->_db->loadResult();

						if (($tmdAwayWins - $tmdAwayLoses) > 0)
						{
							$team->AwayMatchWins ++;
						} else if (($tmdAwayWins - $tmdAwayLoses) < 0)
						{
							$team->AwayMatchLoses ++;
						} else
						{
							$team->AwayMatchDraws ++;
						}
						
					
					} 
					
					$team->HomeWins = $tmdHomeWins;
					$team->HomeLoses = $tmdHomeLoses;
					$team->HomeDraws = $tmdHomeDraws;
					
					$team->HomeLWAT = $tmdHomeStats->HomeLWAT;
					$team->Home100 = ($tmdHomeStats->Home100 < 0) ? -1 : $tmdHomeStats->Home100;
					$team->Home140 = ($tmdHomeStats->Home140 < 0) ? -1 : $tmdHomeStats->Home140;
					$team->Home180 = $tmdHomeStats->Home180;
					$team->HomeHC = $tmdHomeStats->HomeHC;
					
					$team->AwayWins = $tmdAwayWins;
					$team->AwayLoses = $tmdAwayLoses;
					$team->AwayDraws = $tmdAwayDraws;

					$team->AwayLWAT = $tmdAwayStats->AwayLWAT;
					$team->Away100 = ($tmdAwayStats->Away100 < 0) ? -1 : $tmdAwayStats->Away100;
					$team->Away140 = ($tmdAwayStats->Away140 < 0) ? -1 : $tmdAwayStats->Away140;
					$team->Away180 = $tmdAwayStats->Away180;
					$team->AwayHC = $tmdAwayStats->AwayHC;
					
					$team->TotalWins = $tmdHomeWins + $tmdAwayWins;
					$team->TotalLoses = $tmdHomeLoses + $tmdAwayLoses;
					$team->TotalDraws = $tmdHomeDraws + $tmdAwayDraws;
					$team->TotalDiff = $team->TotalWins - $team->TotalLoses;
					
					$team->TotalMatchWins = $team->HomeMatchWins + $team->AwayMatchWins;
					$team->TotalMatchWinPoints = $team->TotalMatchWins * $event->EventPointsWin;
					$team->TotalMatchLoses = $team->HomeMatchLoses + $team->AwayMatchLoses;
					$team->TotalMatchLosePoints = $team->TotalMatchLoses * $event->EventPointsLose;
					$team->TotalMatchDraws = $team->HomeMatchDraws + $team->AwayMatchDraws;
					$team->TotalMatchDrawPoints = $team->TotalMatchDraws * $event->EventPointsDraw;
					
					
					$team->TotalLWAT = $team->HomeLWAT + $team->AwayLWAT;
					$team->Total100 = ($team->Home100 < 0) ? -1 : $team->Home100 + $team->Away100;
					$team->Total140 = ($team->Home140 < 0) ? -1 : $team->Home140 + $team->Away140;
					$team->Total180 = $team->Home180 + $team->Away180;
					$team->TotalHC = ($team->HomeHC > $team->AwayHC) ? $team->HomeHC : $team->AwayHC;
					
					$team->TotalMatches = $team->TotalMatchWins + $team->TotalMatchDraws + $team->TotalMatchLoses;
					$team->TotalPoints = $team->TotalMatchWinPoints + $team->TotalMatchDrawPoints + $team->TotalMatchLosePoints;
					
				}
				
				
				
			}
			

			$teams = JArrayHelper::sortObjects($teams, 'TotalMatchDiff', 1);
			$teams = JArrayHelper::sortObjects($teams, 'TotalPoints', -1);

			$this->_tableTeamData = $teams;

		}
		return $this->_tableTeamData;
	}
	
	public function getSchedulePlayer()
	{
		if (empty( $this->_schedulePlayer ))
		{
			$params = JComponentHelper::getParams('com_matchdart');

			$season = $this->getSeasonID();
			$event = $this->getEventID();
			
		$query = 'SELECT'.
	' M.MatchID,'.
	' M.MatchDate,'.
	' M.MatchResult,'.
	' PH.PlayerName as HomePlayerName,'.
	' PH.PlayerUID as HomePlayerUID,'.
	' PA.PlayerName as AwayPlayerName,'.
	' PA.PlayerUID as AwayPlayerUID,'.
	' M.MatchHomeSets AS HomeSets,'.
	' M.MatchAwaySets AS AwaySets,'.
	' M.MatchHomeLegs AS HomeLegs,'.
	' M.MatchAwayLegs AS AwayLegs,'.
	' V.VenueName AS VenueName'.
		' FROM '.
		' #__md_matches M'.
		' LEFT JOIN #__md_players PH on (PH.PlayerID = M.MatchHomePlayerID)'.
		' LEFT JOIN #__md_players PA on (PA.PlayerID = M.MatchAwayPlayerID)'.
		' LEFT JOIN #__md_venues V on (V.VenueID = M.MatchVenueID)'.
	' WHERE'.
	' M.MatchDate <= CURDATE() AND'.
	' MatchSeasonID LIKE '.$this->_db->Quote( $season ).' AND'.
	' MatchEventID LIKE '.$this->_db->Quote( $event ).
	' ORDER BY'.
	' M.MatchDate DESC';
	$this->_db->setQuery( $query );
	$this->_schedulePlayer = $this->_getList($query);
		}
		
		return $this->_schedulePlayer;

	}
			

}

?>